package com.meizhuang.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.meizhuang.base.BaseController;
import com.meizhuang.entity.Coupon;
import com.meizhuang.param.request.PageRequest;
import com.meizhuang.result.JsonResult;
import com.meizhuang.result.PageResult;
import com.meizhuang.service.CouponService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping(value = "coupon")
public class CouponController extends BaseController {

    @Autowired
    private CouponService couponService;

    @RequestMapping("/list")
    public PageResult<Coupon> list(PageRequest pageRequest){

        Page<Coupon> page = new Page(pageRequest.getPage(), pageRequest.getLimit(), pageRequest.getSortField(), pageRequest.getAscState());
        couponService.selectPage(page);
        return  PageResult.buildSuccess(page.getTotal(), page.getRecords());
    }


    @RequestMapping("/add")
    public JsonResult add(Coupon counpon){

        Map<String,Object> retMap = new LinkedHashMap<>();

        if(StringUtils.isBlank(counpon.getName())){
            retMap.put("msg", "优惠券名称为空");
            return JsonResult.buildError(retMap);
        }

        Lock lock = new ReentrantLock();

        try {
            lock.lock();

            EntityWrapper<Coupon>  couponEntityWrapper= new EntityWrapper<>();
            couponEntityWrapper.eq("name",counpon.getName());
            Coupon coupon = couponService.selectOne(couponEntityWrapper);


            if(null != coupon ){
                retMap.put("msg", "优惠劵名称已存在");
                return JsonResult.buildError(retMap);
            }

            switch (counpon.getUsed()) {
                case 10:
                case 20:
                    break;
                default:{
                    retMap.put("msg", "used 可用于：10店铺优惠券  20商品优惠券");
                    return JsonResult.buildError(retMap);
                }
            }

            Date now =new Date();

            if(now.before( counpon.getStartTime())  ||  now.after(counpon.getEndTime()) ){
                retMap.put("msg", "发放时间无效");
                return JsonResult.buildError(retMap);
            }

            if(now.after(counpon.getValidEndTime()) ){
                retMap.put("msg", "使用时间无效");
                return JsonResult.buildError(retMap);
            }

            counpon.setCreateTime(now);

            couponService.insert(counpon);

        }catch (Exception e){
            retMap.put("msg", e.getMessage());
            return JsonResult.buildError(retMap);
        }finally {
            lock.unlock();
        }
        return JsonResult.buildSuccess(retMap);
    }


    @RequestMapping("/update")
    public JsonResult update(Coupon counpon){

        Map<String,Object> retMap = new LinkedHashMap<>();

        if(null == counpon.getId()){
            retMap.put("msg", "优惠券ID为空");
            return JsonResult.buildError(retMap);
        }

        EntityWrapper<Coupon>  couponEntityWrapper= new EntityWrapper<>();
        couponEntityWrapper.eq("id",counpon.getId());
        Coupon originCoupon = couponService.selectOne(couponEntityWrapper);

        if(null == originCoupon ){
            retMap.put("msg", "优惠劵不存在");
            return JsonResult.buildError(retMap);
        }

        Date now =new Date();

        if(now.before( counpon.getStartTime())  ||  now.after(counpon.getEndTime()) ){
            retMap.put("msg", "发放时间无效");
            return JsonResult.buildError(retMap);
        }

        if(now.before( counpon.getValidStartTime())  ||  now.after(counpon.getValidEndTime()) ){
            retMap.put("msg", "使用时间无效");
            return JsonResult.buildError(retMap);
        }

        couponService.updateById(counpon);

        return JsonResult.buildSuccess(retMap);
    }

    @RequestMapping("/delete")
    public JsonResult delete(Coupon counpon){

        Map<String,Object> retMap = new LinkedHashMap<>();

        if(null == counpon.getId()){
            retMap.put("msg", "优惠券ID为空");
            return JsonResult.buildError(retMap);
        }


        couponService.deleteById(counpon.getId());

        return JsonResult.buildSuccess(retMap);
    }
}
